import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;

/**
 * Displays the various risks and mitigations,
 * as well as actions that the controller can perform
 * to "Mitigate"
 * @author Henry Yuen
 *
 */
public class RiskMitigationPanel extends JPanel implements ActionListener {

	private JTable riskMitigationTable;	//displays the risks with their associated mitigations
	private RiskMitigationTableModel riskMitigationTableModel;
	private JButton implementMitigationButton; //allows the user to implement a mitigation
	private JButton notifyOtherUsersButton;	//allows the user to notify other users about a particular risk
	private JButton moreInfoButton;

	private class RiskMitigationTableModel extends AbstractTableModel {
		private String[] columnNames = {"Priority","Risk","Mitigation","Severity"};

		private Object[][] data = {{"1","Plane Crash!","Hologram!","Uber Severe!"}};

   	// Essential functions
        public int getColumnCount()
        {
        	return columnNames.length;
        }

        public int getRowCount()
        {
        	if (data == null) return 0;
        	return data.length;
        }

        public String getColumnName(int col)
        {
        	return columnNames[col];
        }

        // Returns the object at the indicated position
        public Object getValueAt(int row, int col)
        {
        	if (data == null) return null;
        	return data[row][col];
        }

        // Turns booleans into checkboxes.  Don't ask.  I don't know how it works myself.
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        // The table cannot be edited by the user, no matter what.
        public boolean isCellEditable(int row, int col)
        {
        	return false;
        }

        // Edits the value at the indicated position
        public void setValueAt(Object value, int row, int col)
        {
        	data[row][col] = value;
        }
    }

	/**
	 * Constructor, initializes the UI components
	 */
	public RiskMitigationPanel() {

		initUI();
	}

	/**
	 * Initializes the UI components
	 */
	private void initUI() {

		//set a nice border
		TitledBorder border = BorderFactory.createTitledBorder("Risks & Mitigations");
		setBorder(border);

		//
//		setLayout(new GridBagLayout());
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

		riskMitigationTableModel = new RiskMitigationTableModel();
		riskMitigationTable = new JTable(riskMitigationTableModel);

		add(new JScrollPane(riskMitigationTable));

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));

		//implementMitigationButton
		implementMitigationButton = new JButton("Implement Mitigation");
		implementMitigationButton.addActionListener(this);

		buttonPanel.add(implementMitigationButton);

		notifyOtherUsersButton = new JButton("Notify Other Users");
		notifyOtherUsersButton.addActionListener(this);

		buttonPanel.add(notifyOtherUsersButton);

		moreInfoButton = new JButton("Show Risk Details");
		moreInfoButton.addActionListener(this);

		buttonPanel.add(moreInfoButton);

		add(buttonPanel);
	}


	/**
	 * Responds to the button actions
	 * @param e Holds the event
	 */
	public void actionPerformed(ActionEvent e) {

	}

}
